from books.models import Book, BookNote 
from django.shortcuts import render_to_response, get_object_or_404 
from django.template import RequestContext
from books.forms import BookForm, BookNoteForm
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, Http404
from django.views.decorators.csrf import csrf_exempt, csrf_protect
from django.contrib.auth.decorators import login_required

   
@csrf_exempt 
def add(request):
    bookform = BookForm(request.POST or None)
    next = 'no'
    if bookform.is_valid():
        book = bookform.save(commit = False )
        book.user = request.user
        book.save()
        next = 'yes'
    return render_to_response (
        'books/add_book.html',
        {
         'bookform' : bookform,
         'next': next,
        },
        context_instance = RequestContext(request)
        )  

@csrf_exempt
def update(request, book_id, dontvalidate):
    book = get_object_or_404(Book, id = book_id)
    if dontvalidate == 'yes':
        bookform = BookForm(instance = book)
        next = 'no'
    else:
        bookform = BookForm(request.POST, instance = book )
    if bookform.is_valid():
        book.save() 
        next = 'yes'
    return render_to_response(
        'books/update_book.html',
        {
        'book_id':book_id,
        'bookform':bookform,
        'next': next,
        'dontvalidate': dontvalidate,
        },
        context_instance = RequestContext(request)
        )

@csrf_exempt
def add_note(request, book_id, dontvalidate):
    saveok = 'no'
    book = get_object_or_404(Book, id = book_id)
    if dontvalidate == 'yes':
        booknoteform = BookNoteForm()
    else:
        booknoteform = BookNoteForm(request.POST or None)
    if booknoteform.is_valid():
        note = booknoteform.save(commit= False)
        note.book = book
        note.save()
        saveok = 'yes'
        booknoteform = BookNoteForm(None)
    book_notes = BookNote.objects.filter(book__user = request.user, book__id =book_id)
    return render_to_response(
        'books/add_booknote.html',
        {
        'request': request,
        'dontvalidate' : dontvalidate,
        'book': book,
        'booknoteform':booknoteform,
        'saveok': saveok,
        'book_notes': book_notes,
        },
        context_instance = RequestContext(request)
        )

@csrf_exempt
def update_note(request, note_id, dontvalidate):
    note = get_object_or_404(BookNote, id = note_id)
    next = 'no'
    if dontvalidate == 'yes':
        noteform = BookNoteForm(instance = note)
    else:
        if request.method  == 'POST':
            noteform = BookNoteForm(request.POST, instance = note )
        else:
            noteform = BookNoteForm(instance = note )
        if noteform.is_valid():
            note.save() 
            next = 'yes'
    return render_to_response(
        'books/update_note.html',
        {
         'note_id':note_id,
         'noteform':noteform,
         'next': next,
         'dontvalidate': dontvalidate,
        },
        context_instance = RequestContext(request)
        )
